package com.zhangsun.quartz.controller;

import com.zhangsun.base.bean.ResponseData;
import com.zhangsun.common.utils.ResponseUtil;
import com.zhangsun.dto.QuartzDto;
import com.zhangsun.service.QuartzJobService;
import com.zhangsun.vo.QuartzEditVo;
import com.zhangsun.vo.QuartzVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private QuartzJobService quartzJobService;

    /**
     * 获取所有任务
     *
     * @return 任务
     */
    @GetMapping("/findAll")
    @RequiresPermissions("system:task:view")
    public ResponseData findAll() {
        List<QuartzDto> jobs = quartzJobService.findAll();
        return ResponseUtil.success(jobs);
    }

    /**
     * 添加任务
     *
     * @param vo 任务
     */
    @PostMapping("/create")
    @RequiresPermissions("system:task:edit")
    public ResponseData create(@Validated QuartzEditVo vo) {
        quartzJobService.create(vo);
        return ResponseUtil.success("添加成功");
    }

    /**
     * 更新任务
     *
     * @param vo 任务
     */
    @PostMapping("/update")
    @RequiresPermissions("system:task:edit")
    public ResponseData update(@Validated QuartzEditVo vo) {
        quartzJobService.update(vo);
        return ResponseUtil.success("更新成功");
    }

    /**
     * 暂停任务
     *
     * @param vo 任务
     */
    @PostMapping("/pause")
    @RequiresPermissions("system:task:status")
    public ResponseData pause(@Validated QuartzVo vo) {
        quartzJobService.pause(vo);
        return ResponseUtil.success("暂停成功");
    }

    /**
     * 重新开始任务
     *
     * @param vo 任务
     */
    @PostMapping("/resume")
    @RequiresPermissions("system:task:status")
    public ResponseData resume(@Validated QuartzVo vo) {
        quartzJobService.resume(vo);
        return ResponseUtil.success("重新开始成功");
    }

    /**
     * 删除任务
     *
     * @param vo 任务
     */
    @PostMapping("/delete")
    @RequiresPermissions("system:task:delete")
    public ResponseData delete(@Validated QuartzVo vo) {
        quartzJobService.delete(vo);
        return ResponseUtil.success("删除成功");
    }

}
